package util;

import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class CommonUtil {
	public static String createAjaxResultXML(String strTable, String strId) {
		try {
			DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element root = doc.createElement(strTable);
			doc.appendChild(root);
			Element eIdElement = doc.createElement("id");
			root.appendChild(eIdElement);
			Text tIdText = doc.createTextNode(strId);
			eIdElement.appendChild(tIdText);

			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			// create string from xml tree
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			String xmlString = sw.toString();

			return xmlString;

		} catch (Exception e) {
			return "error";
		}
	}

	public static String createAjaxResultXML(String strTable,
			List<String> list, List<String> listId) {
		try {
			DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element root = doc.createElement(strTable);
			doc.appendChild(root);
			Element eSizeElement = doc.createElement("size");
			root.appendChild(eSizeElement);
			Text tSizeText = doc.createTextNode(String.valueOf(list.size()));
			eSizeElement.appendChild(tSizeText);
			for (int i = 0; i < list.size(); i++) {
				Element eIdElement = doc.createElement("id" + i);
				root.appendChild(eIdElement);
				Text tIdText = doc.createTextNode(listId.get(i));
				eIdElement.appendChild(tIdText);

				Element eNameElement = doc.createElement("name" + i);
				root.appendChild(eNameElement);
				Text tNameText = doc.createTextNode(list.get(i));
				eNameElement.appendChild(tNameText);
			}
			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			// create string from xml tree
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			String xmlString = sw.toString();

			return xmlString;
		} catch (Exception e) {
			return "error";
		}
	}
	public static String createAjaxResultTagXML(String strError,String strFormattedTag,String strOldTagList,String strNewTagList) {
		try {
			DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element root = doc.createElement("tag");
			doc.appendChild(root);
			Element eError = doc.createElement("error");
			root.appendChild(eError);
			Text tError = doc.createTextNode(strError);
			eError.appendChild(tError);
			Element eFormattedTag = doc.createElement("formattedTag");
			root.appendChild(eFormattedTag);
			Text tFormattedTag = doc.createTextNode(strFormattedTag);
			eFormattedTag.appendChild(tFormattedTag);
			Element eOldTagList = doc.createElement("oldTagList");
			root.appendChild(eOldTagList);
			Text tOldTagList = doc.createTextNode(strOldTagList);
			eOldTagList.appendChild(tOldTagList);
			Element eNewTagList = doc.createElement("newTagList");
			root.appendChild(eNewTagList);
			Text tNewTagList = doc.createTextNode(strNewTagList);
			eNewTagList.appendChild(tNewTagList);
			
			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			// create string from xml tree
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			String xmlString = sw.toString();

			return xmlString;
		} catch (Exception e) {
			return "error";
		}
	}


}
